package com.gxp.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.gxp.mapper.OrderMapper;
import com.gxp.pojo.Order;
import com.gxp.pojo.WXOrder;
import com.gxp.service.OrderService;
import com.gxp.util.common.GXPConstant;

@Service
@Transactional
public class OrderServiceImpl implements OrderService {
	
	Logger log = Logger.getLogger(OrderServiceImpl.class);
	
	@Autowired
	private OrderMapper orderMapper;

	@Override
	public List<Order> findBuyerOrders(Map<String, Object> param) {
		return orderMapper.findBuyerOrders(param);
	}

	@Override
	public Order findByOrderId(int orderId) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put(GXPConstant.MAPPER_KEY_ORDER_ID, orderId);
		List<Order> orders = orderMapper.findBuyerOrders(param);
		return orders.get(0);
	}

	@Override
	public void updateOrderStatus(Map<String, Object> param) {
		orderMapper.updateOrderStatus(param);
	}

	@Override
	public void insertOrder(Order order) {
		orderMapper.insertOrder(order);		
	}

	@Override
	public List<Order> findByUserId(String userId) {
		Map<String, Object> param = new HashMap<String, Object>();
		param.put(GXPConstant.MAPPER_KEY_USER_ID, userId);
		List<Order> orders = orderMapper.findBuyerOrders(param);
		return orders;
	}

	@Override
	public void updateBuyerOrder(Order order) {
		orderMapper.updateBuyerOrder(order);
	}

	@Override
	public void delOrder(Map<String, Object> param) {
		orderMapper.delBuyerOrder(param);
	}

	@Override
	public WXOrder getWxOrder(Map<String, Object> param) {
		List<WXOrder> result =  orderMapper.selectOrderBuyerInfoForWx(param);
		if (result != null && !result.isEmpty()) {
			return result.get(0);
		}
		return null;
	}	
	
	public List<WXOrder> findOrdersForWx(Map<String, Object> param) {
		return orderMapper.selectOrderForWx(param);
	}
}
